using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MacomberMap.Common.Internals;
using MacomberMap.Common.User_Interfaces.Components;

namespace MacomberMap.Common.User_Interfaces.Database
{
 
    /// <summary>
    /// This class shows a table selection dialog
    /// </summary>    
    public partial class frmTableSelector : Form
    {
        /// <summary>
        /// Return the collection of selected tables
        /// </summary>
        public string[] SelectedTables
        {
            get
            {
                string[] OutString = new string[lvRepsonses.CheckedItems.Count];
                for (int a = 0; a < lvRepsonses.CheckedItems.Count; a++)
                    OutString[a] = lvRepsonses.CheckedItems[a].Text as String;
                return OutString;
            }
        }

        /// <summary>
        /// Initialize a new table selector
        /// </summary>
        /// <param name="OutTables">The tables to be added</param>
        public frmTableSelector(DataTable OutTables)
        {
            InitializeComponent();

            //Start by seting up our columns
            foreach (DataColumn c in OutTables.Columns)
                lvRepsonses.Columns.Add(c.ColumnName, MM_Repository.TitleCase(c.ColumnName));

            //Now, add in the rows
            foreach (DataRow dR in OutTables.Rows)
            {
                ListViewItem NewItem = lvRepsonses.Items.Add(MM_Repository.TitleCase((string)dR[0]));
                for (int a = 1; a < OutTables.Columns.Count; a++)
                    if (dR[a] is String)
                        NewItem.SubItems.Add(MM_Repository.TitleCase((string)dR[a]));
                    else if (dR[a] is Decimal)
                        NewItem.SubItems.Add(((Decimal)dR[a]).ToString("#,##0"));
                    else if (dR[a] is DBNull)
                        NewItem.SubItems.Add("");
                    else
                        NewItem.SubItems.Add(MM_Repository.TitleCase(dR[a].ToString()));
            }
            lvRepsonses.Sort();
            for (int a = 0; a < lvRepsonses.Columns.Count; a++)
                lvRepsonses.AutoResizeColumn(a, a < 2 ? ColumnHeaderAutoResizeStyle.ColumnContent : ColumnHeaderAutoResizeStyle.HeaderSize);
            
        }
        
        /// <summary>
        /// Select all tables
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAll_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvI in lvRepsonses.Items)
                lvI.Checked = true;
        }

        /// <summary>
        /// Unselect all tables
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNone_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvI in lvRepsonses.Items)
                lvI.Checked = false;
        }

        /// <summary>
        /// Invert the selection of all tables
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInvert_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvI in lvRepsonses.Items)
                lvI.Checked ^= true;
        }

        /// <summary>
        /// Go through all tables, and select only those that are contained in the element type list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnModel_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvI in lvRepsonses.Items)
                lvI.Checked = MM_Overall_Display.ElemTypes.ContainsKey(lvI.Text.Substring(3)) || lvI.Text == "Mm_Elements";
                    
        }
    }
}